package cn.fintecher.pangolin.service.report.request;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Data
public class DailyCollectionStatisticsRequest {

    @ApiModelProperty(value = "部门code")
    private String departCode;

    @ApiModelProperty(value = "催收员ID集合")
    private String userIds;

    @ApiModelProperty(value = "开始时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date startDate;

    @ApiModelProperty(value = "结束时间")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date endDate;

    public BoolQueryBuilder generateBoolQueryBuilder(){
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if(StringUtils.isNotBlank(departCode)){
            boolQueryBuilder.must(QueryBuilders.matchPhrasePrefixQuery("departmentCode",this.departCode));
        }
        if(StringUtils.isNotBlank(this.userIds)){
            String ids [] = StringUtils.split(userIds,",");
            boolQueryBuilder.must(QueryBuilders.termsQuery("collectorId.keyword",ids));
        }
        if(Objects.nonNull(startDate)){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("statisticsDate").gte(startDate.getTime()));
        }
        if(Objects.nonNull(endDate)){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("statisticsDate").lte(endDate.getTime()));
        }
        return boolQueryBuilder;
    }
}
